home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0192.ARJ / WINDOW.C < prev    next >
Text File  |  1991-10-22  |  15KB  |  487 lines

  1. /* ---------- window.c ------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. WINDOW inFocus = NULL;
  6.  
  7. int foreground, background;   /* current video colors */
  8.  
  9. static void TopLine(WINDOW, int, RECT);
  10.  
  11. /* --------- create a window ------------ */
  12. WINDOW CreateWindow(
  13.     CLASS class,              /* class of this window       */
  14.     char *ttl,                /* title or NULL              */
  15.     int left, int top,        /* upper left coordinates     */
  16.     int height, int width,    /* dimensions                 */
  17.     void *extension,          /* pointer to additional data */
  18.     WINDOW parent,            /* parent of this window      */
  19.     int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
  20.     int attrib)               /* window attribute           */
  21. {
  22.     WINDOW wnd = calloc(1, sizeof(struct window));
  23.     get_videomode();
  24.     if (wnd != NULL)    {
  25.         int base;
  26.         /* ----- height, width = -1: fill the screen ------- */
  27.         if (height == -1)
  28.             height = SCREENHEIGHT;
  29.         if (width == -1)
  30.             width = SCREENWIDTH;
  31.         /* ----- coordinates -1, -1 = center the window ---- */
  32.         if (left == -1)
  33.             wnd->rc.lf = (SCREENWIDTH-width)/2;
  34.         else
  35.             wnd->rc.lf = left;
  36.         if (top == -1)
  37.             wnd->rc.tp = (SCREENHEIGHT-height)/2;
  38.         else
  39.             wnd->rc.tp = top;
  40.         wnd->attrib = attrib;
  41.         if (ttl != NULL)
  42.             AddAttribute(wnd, HASTITLEBAR);
  43.         if (wndproc == NULL)
  44.             wnd->wndproc = classdefs[class].wndproc;
  45.         else
  46.             wnd->wndproc = wndproc;
  47.         /* ---- derive attributes of base classes ---- */
  48.         base = class;
  49.         while (base != -1)    {
  50.             AddAttribute(wnd, classdefs[base].attrib);
  51.             base = classdefs[base].base;
  52.         }
  53.         if (parent && !TestAttribute(wnd, NOCLIP))    {
  54.             /* -- keep upper left within borders of parent - */
  55.             wnd->rc.lf = max(wnd->rc.lf,GetClientLeft(parent));
  56.             wnd->rc.tp = max(wnd->rc.tp,GetClientTop(parent));
  57.         }
  58.         wnd->class = class;
  59.         wnd->extension = extension;
  60.         wnd->rc.rt = GetLeft(wnd)+width-1;
  61.         wnd->rc.bt = GetTop(wnd)+height-1;
  62.         wnd->ht = height;
  63.         wnd->wd = width;
  64.         if (ttl != NULL)
  65.             InsertTitle(wnd, ttl);
  66.         wnd->nextfocus = wnd->prevfocus = wnd->dFocus = NULL;
  67.         wnd->parent = parent;
  68.         wnd->oldcondition = wnd->condition = ISRESTORED;
  69.         wnd->RestoredRC = wnd->rc;
  70.         wnd->PrevKeyboard = wnd->PrevMouse = NULL;
  71.         InitWindowColors(wnd);
  72.         SendMessage(wnd, CREATE_WINDOW, 0, 0);
  73.         if (isVisible(wnd))
  74.             SendMessage(wnd, SHOW_WINDOW, 0, 0);
  75.     }
  76.     return wnd;
  77. }
  78.  
  79. /* -------- add a title to a window --------- */
  80. void AddTitle(WINDOW wnd, char *ttl)
  81. {
  82.     InsertTitle(wnd, ttl);
  83.     SendMessage(wnd, BORDER, 0, 0);
  84. }
  85.  
  86. /* ----- insert a title into a window ---------- */
  87. void InsertTitle(WINDOW wnd, char *ttl)
  88. {
  89.     if ((wnd->title=realloc(wnd->title,strlen(ttl)+1)) != NULL)
  90.         strcpy(wnd->title, ttl);
  91. }
  92.  
  93. static unsigned char line[300];
  94.  
  95. /* ------ write a line to video window client area ------ */
  96. void writeline(WINDOW wnd, char *str, int x, int y, int pad)
  97. {
  98.     char *cp;
  99.     int len;
  100.     int dif;
  101.     char *wline = calloc(1, 200);
  102.  
  103.     if (wline != NULL)    {
  104.         len = LineLength(str);
  105.         dif = strlen(str) - len;
  106.         strncpy(wline, str, ClientWidth(wnd) + dif);
  107.         if (pad)    {
  108.             cp = wline+strlen(wline);
  109.             while (len++ < ClientWidth(wnd)-x)
  110.                 *cp++ = ' ';
  111.         }
  112.         wputs(wnd, wline, x, y);
  113.         free(wline);
  114.     }
  115. }
  116.  
  117. RECT AdjustRectangle(WINDOW wnd, RECT rc)
  118. {
  119.     /* -------- adjust the rectangle ------- */
  120.     if (TestAttribute(wnd, HASBORDER))    {
  121.         if (RectLeft(rc) == 0)
  122.             --rc.rt;
  123.         else if (RectLeft(rc) < RectRight(rc) &&
  124.                 RectLeft(rc) < WindowWidth(wnd)+1)
  125.             --rc.lf;
  126.     }
  127.     if (TestAttribute(wnd, HASBORDER | HASTITLEBAR))    {
  128.         if (RectTop(rc) == 0)
  129.             --rc.bt;
  130.         else if (RectTop(rc) < RectBottom(rc) &&
  131.                 RectTop(rc) < WindowHeight(wnd)+1)
  132.             --rc.tp;
  133.     }
  134.     RectRight(rc) = max(RectLeft(rc),
  135.                         min(RectRight(rc),WindowWidth(wnd)));
  136.     RectBottom(rc) = max(RectTop(rc),
  137.                         min(RectBottom(rc),WindowHeight(wnd)));
  138.     return rc;
  139. }
  140.  
  141. /* -------- display a window's title --------- */
  142. void DisplayTitle(WINDOW wnd, RECT *rcc)
  143. {
  144.     if (GetTitle(wnd) != NULL)    {
  145.         int tlen = min(strlen(GetTitle(wnd)), WindowWidth(wnd)-2);
  146.         int tend = WindowWidth(wnd)-3-BorderAdj(wnd);
  147.         RECT rc;
  148.  
  149.         if (rcc == NULL)
  150.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  151.         else
  152.             rc = *rcc;
  153.         rc = AdjustRectangle(wnd, rc);
  154.  
  155.         if (SendMessage(wnd, TITLE, (PARAM) rcc, 0))    {
  156.             if (wnd == inFocus)    {
  157.                 foreground = cfg.clr[TITLEBAR] [HILITE_COLOR] [FG];
  158.                 background = cfg.clr[TITLEBAR] [HILITE_COLOR] [BG];
  159.             }
  160.             else    {
  161.                 foreground = cfg.clr[TITLEBAR] [STD_COLOR] [FG];
  162.                 background = cfg.clr[TITLEBAR] [STD_COLOR] [BG];
  163.             }
  164.             memset(line,' ',WindowWidth(wnd));
  165.             if (wnd->condition != ISMINIMIZED)
  166.                 strncpy(line + ((WindowWidth(wnd)-2 - tlen) / 2),
  167.                     wnd->title, tlen);
  168.             if (TestAttribute(wnd, CONTROLBOX))
  169.                 line[2-BorderAdj(wnd)] = CONTROLBOXCHAR;
  170.             if (TestAttribute(wnd, MINMAXBOX))    {
  171.                 switch (wnd->condition)    {
  172.                     case ISRESTORED:
  173.                         line[tend+1] = MAXPOINTER;
  174.                         line[tend]   = MINPOINTER;
  175.                         break;
  176.                     case ISMINIMIZED:
  177.                         line[tend+1] = MAXPOINTER;
  178.                         break;
  179.                     case ISMAXIMIZED:
  180.                         line[tend]   = MINPOINTER;
  181.                         line[tend+1] = RESTOREPOINTER;
  182.                         break;
  183.                     default:
  184.                         break;
  185.                 }
  186.             }
  187.             line[RectRight(rc)+1] = line[tend+3] = '\0';
  188.             writeline(wnd, line+RectLeft(rc),
  189.                            RectLeft(rc)+BorderAdj(wnd),
  190.                            0,
  191.                            FALSE);
  192.         }
  193.     }
  194. }
  195.  
  196. /* --- display right border shadow character of a window --- */
  197. static void near shadow_char(WINDOW wnd, int y)
  198. {
  199.     int fg = foreground;
  200.     int bg = background;
  201.     int x = WindowWidth(wnd);
  202.     int c = videochar(GetLeft(wnd)+x, GetTop(wnd)+y);
  203.  
  204.     if (TestAttribute(wnd, SHADOW) == 0)
  205.         return;
  206.     foreground = LIGHTGRAY;
  207.     background = BLACK;
  208.     wputch(wnd, c, x, y);
  209.     foreground = fg;
  210.     background = bg;
  211. }
  212.  
  213. /* --- display the bottom border shadow line for a window -- */
  214. static void near shadowline(WINDOW wnd, RECT rc)
  215. {
  216.     int i;
  217.     int y = GetBottom(wnd)+1;
  218.     int fg = foreground;
  219.     int bg = background;
  220.  
  221.     if ((TestAttribute(wnd, SHADOW)) == 0)
  222.         return;
  223.     for (i = 0; i < WindowWidth(wnd)+1; i++)
  224.         line[i] = videochar(GetLeft(wnd)+i, y);
  225.     line[i] = '\0';
  226.     foreground = LIGHTGRAY;
  227.     background = BLACK;
  228.     line[RectRight(rc)+1] = '\0';
  229.     if (RectLeft(rc) == 0)
  230.         rc.lf++;
  231.     ClipString++;
  232.     wputs(wnd, line+RectLeft(rc), RectLeft(rc),
  233.         WindowHeight(wnd));
  234.     --ClipString;
  235.     foreground = fg;
  236.     background = bg;
  237. }
  238.  
  239. /* ------- display a window's border ----- */
  240. void RepaintBorder(WINDOW wnd, RECT *rcc)
  241. {
  242.     int y;
  243.     unsigned int lin, side, ne, nw, se, sw;
  244.     RECT rc, clrc;
  245.  
  246.     if (!TestAttribute(wnd, HASBORDER))
  247.         return;
  248.     if (rcc == NULL)    {
  249.         rc = RelativeWindowRect(wnd, WindowRect(wnd));
  250.         if (TestAttribute(wnd, SHADOW))    {
  251.             rc.rt++;
  252.             rc.bt++;
  253.         }
  254.     }
  255.     else
  256.         rc = *rcc;
  257.     clrc = AdjustRectangle(wnd, rc);
  258.  
  259.     if (wnd == inFocus)    {
  260.         lin  = FOCUS_LINE;
  261.         side = FOCUS_SIDE;
  262.         ne   = FOCUS_NE;
  263.         nw   = FOCUS_NW;
  264.         se   = FOCUS_SE;
  265.         sw   = FOCUS_SW;
  266.     }
  267.     else    {
  268.         lin  = LINE;
  269.         side = SIDE;
  270.         ne   = NE;
  271.         nw   = NW;
  272.         se   = SE;
  273.         sw   = SW;
  274.     }
  275.     line[WindowWidth(wnd)] = '\0';
  276.     /* ---------- window title ------------ */
  277.     if (TestAttribute(wnd, HASTITLEBAR))
  278.         if (RectTop(rc) == 0)
  279.             if (RectLeft(rc) < WindowWidth(wnd)-BorderAdj(wnd))
  280.                 DisplayTitle(wnd, &rc);
  281.     foreground = FrameForeground(wnd);
  282.     background = FrameBackground(wnd);
  283.     /* -------- top frame corners --------- */
  284.     if (RectTop(rc) == 0)    {
  285.         if (RectLeft(rc) == 0)
  286.             wputch(wnd, nw, 0, 0);
  287.         if (RectLeft(rc) < WindowWidth(wnd))    {
  288.             if (RectRight(rc) >= WindowWidth(wnd)-1)
  289.                 wputch(wnd, ne, WindowWidth(wnd)-1, 0);
  290.             TopLine(wnd, lin, clrc);
  291.         }
  292.     }
  293.  
  294.     /* ----------- window body ------------ */
  295.     for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  296.         int ch;
  297.         if (y == 0 || y >= WindowHeight(wnd)-1)
  298.             continue;
  299.         if (RectLeft(rc) == 0)
  300.             wputch(wnd, side, 0, y);
  301.         if (RectLeft(rc) < WindowWidth(wnd) &&
  302.                 RectRight(rc) >= WindowWidth(wnd)-1)    {
  303.             if (TestAttribute(wnd, VSCROLLBAR))
  304.                 ch = (    y == 1 ? UPSCROLLBOX      :
  305.                           y == WindowHeight(wnd)-2  ?
  306.                                 DOWNSCROLLBOX       :
  307.                           y-1 == wnd->VScrollBox    ?
  308.                                 SCROLLBOXCHAR       :
  309.                           SCROLLBARCHAR );
  310.             else
  311.                 ch = side;
  312.             wputch(wnd, ch, WindowWidth(wnd)-1, y);
  313.         }
  314.         if (RectRight(rc) == WindowWidth(wnd))
  315.             shadow_char(wnd, y);
  316.     }
  317.  
  318.     if (RectTop(rc) <= WindowHeight(wnd)-1 &&
  319.             RectBottom(rc) >= WindowHeight(wnd)-1)    {
  320.         /* -------- bottom frame corners ---------- */
  321.         if (RectLeft(rc) == 0)
  322.             wputch(wnd, sw, 0, WindowHeight(wnd)-1);
  323.         if (RectLeft(rc) < WindowWidth(wnd) &&
  324.                 RectRight(rc) >= WindowWidth(wnd)-1)
  325.             wputch(wnd, se, WindowWidth(wnd)-1,
  326.                 WindowHeight(wnd)-1);
  327.  
  328.  
  329.         if (wnd->StatusBar == NULL)    {
  330.             /* ----------- bottom line ------------- */
  331.             memset(line,lin,WindowWidth(wnd)-1);
  332.             if (TestAttribute(wnd, HSCROLLBAR))    {
  333.                 line[0] = LEFTSCROLLBOX;
  334.                 line[WindowWidth(wnd)-3] = RIGHTSCROLLBOX;
  335.                 memset(line+1, SCROLLBARCHAR, WindowWidth(wnd)-4);
  336.                 line[wnd->HScrollBox] = SCROLLBOXCHAR;
  337.             }
  338.             line[WindowWidth(wnd)-2] = line[RectRight(rc)] = '\0';
  339.             if (RectLeft(rc) != RectRight(rc) ||
  340.             (RectLeft(rc) && RectLeft(rc) < WindowWidth(wnd)-1))
  341.                 writeline(wnd,
  342.                     line+(RectLeft(clrc)),
  343.                     RectLeft(clrc)+1,
  344.                     WindowHeight(wnd)-1,
  345.                     FALSE);
  346.         }
  347.         if (RectRight(rc) == WindowWidth(wnd))
  348.             shadow_char(wnd, WindowHeight(wnd)-1);
  349.     }
  350.     if (RectBottom(rc) == WindowHeight(wnd))
  351.         /* ---------- bottom shadow ------------- */
  352.         shadowline(wnd, rc);
  353. }
  354.  
  355. static void TopLine(WINDOW wnd, int lin, RECT rc)
  356. {
  357.     if (TestAttribute(wnd, HASMENUBAR))
  358.         return;
  359.     if (TestAttribute(wnd, HASTITLEBAR) && GetTitle(wnd))
  360.         return;
  361.     if (RectLeft(rc) == 0)    {
  362.         RectLeft(rc) += BorderAdj(wnd);
  363.         RectRight(rc) += BorderAdj(wnd);
  364.     }
  365.     if (RectRight(rc) < WindowWidth(wnd)-1)
  366.         RectRight(rc)++;
  367.  
  368.     if (RectLeft(rc) < RectRight(rc))    {
  369.         /* ----------- top line ------------- */
  370.         memset(line,lin,WindowWidth(wnd)-1);
  371.         if (TestAttribute(wnd, CONTROLBOX))    {
  372.             strncpy(line+1, "   ", 3);
  373.             *(line+2) = CONTROLBOXCHAR;
  374.         }
  375.         line[RectRight(rc)] = '\0';
  376.         writeline(wnd, line+RectLeft(rc),
  377.             RectLeft(rc), 0, FALSE);
  378.     }
  379. }
  380.  
  381. /* ------ clear the data space of a window -------- */
  382. void ClearWindow(WINDOW wnd, RECT *rcc, int clrchar)
  383. {
  384.     if (isVisible(wnd))    {
  385.         int y;
  386.         RECT rc;
  387.  
  388.         if (rcc == NULL)
  389.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  390.         else
  391.             rc = *rcc;
  392.  
  393.         if (RectLeft(rc) == 0)
  394.             RectLeft(rc) = BorderAdj(wnd);
  395.         if (RectRight(rc) > WindowWidth(wnd)-1)
  396.             RectRight(rc) = WindowWidth(wnd)-1;
  397.         SetStandardColor(wnd);
  398.         memset(line, clrchar, sizeof line);
  399.         line[RectRight(rc)+1] = '\0';
  400.         for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  401.             if (y < TopBorderAdj(wnd) ||
  402.                     y > ClientHeight(wnd)+
  403.                         (TestAttribute(wnd, HASMENUBAR) ? 1 : 0))
  404.                 continue;
  405.             writeline(wnd,
  406.                 line+(RectLeft(rc)),
  407.                 RectLeft(rc),
  408.                 y,
  409.                 FALSE);
  410.         }
  411.     }
  412. }
  413.  
  414. /* -- adjust a window's rectangle to clip it to its parent - */
  415. static RECT near ClipRect(WINDOW wnd)
  416. {
  417.     RECT rc;
  418.     rc = wnd->rc;
  419.     if (TestAttribute(wnd, SHADOW))    {
  420.         RectBottom(rc)++;
  421.         RectRight(rc)++;
  422.     }
  423.     return ClipRectangle(wnd, rc);
  424. }
  425.  
  426. /* -- get the video memory that is to be used by a window -- */
  427. void GetVideoBuffer(WINDOW wnd)
  428. {
  429.     RECT rc;
  430.     int ht;
  431.     int wd;
  432.  
  433.     rc = ClipRect(wnd);
  434.     ht = RectBottom(rc) - RectTop(rc) + 1;
  435.     wd = RectRight(rc) - RectLeft(rc) + 1;
  436.     wnd->videosave = realloc(wnd->videosave, (ht * wd * 2));
  437.     get_videomode();
  438.     if (wnd->videosave != NULL)
  439.         getvideo(rc, wnd->videosave);
  440. }
  441.  
  442. /* --- restore the video memory that was used by a window -- */
  443. void RestoreVideoBuffer(WINDOW wnd)
  444. {
  445.     if (wnd->videosave != NULL)    {
  446.         RECT rc;
  447.         rc = ClipRect(wnd);
  448.         storevideo(rc, wnd->videosave);
  449.         free(wnd->videosave);
  450.         wnd->videosave = NULL;
  451.     }
  452. }
  453.  
  454. /* ------ compute the logical line length of a window ------ */
  455. int LineLength(char *ln)
  456. {
  457.     int len = strlen(ln);
  458.     char *cp = ln;
  459.     while ((cp = strchr(cp, CHANGECOLOR)) != NULL)    {
  460.         cp++;
  461.         len -= 3;
  462.     }
  463.     cp = ln;
  464.     while ((cp = strchr(cp, RESETCOLOR)) != NULL)    {
  465.         cp++;
  466.         --len;
  467.     }
  468.     return len;
  469. }
  470.  
  471. void InitWindowColors(WINDOW wnd)
  472. {
  473.     int fbg,col;
  474.     /* ---------- set the colors ---------- */
  475.     for (fbg = 0; fbg < 2; fbg++)
  476.         for (col = 0; col < 4; col++)
  477.             wnd->WindowColors[col][fbg] = cfg.clr[GetClass(wnd)][col][fbg];
  478. }
  479.  
  480. void PutWindowChar(WINDOW wnd, int c, int x, int y)
  481. {
  482.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))
  483.         wputch(wnd, c, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  484. }
  485.  
  486.  
  487.